package com.tanhua.app.controller;

import com.tanhua.app.Service.UserInfoService;
import com.tanhua.app.Service.UserService;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.model.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    //    登录---获取验证码
//    POST  /user/login
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map<String, String> map) {
        userService.sendCode(map.get("phone"));
        return ResponseEntity.ok(null);
    }

    //    登录---验证码校验
    //POST /user/loginVerification
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String, String> map) {
        String verificationCode = map.get("verificationCode");
        String mobile = map.get("phone");
        Map<String, Object> retmap = userService.loginVerification(mobile, verificationCode);
        return ResponseEntity.ok(retmap);
    }

    //    首次登录---完善资料
//    POST  /user/loginReginfo
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo) {
        Long id = UserHolder.getUserId();
        userInfo.setId(id);
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    //    首次登录---补充头像
//    POST    /user/loginReginfo/head
    @PostMapping("/loginReginfo/head")
    public ResponseEntity loginHead(MultipartFile headPhoto) throws Exception {
        Long id = UserHolder.getUserId();
        userInfoService.updateHead(headPhoto, id);
        return ResponseEntity.ok(null);
    }

}
